package com.ruoyi.biz.api.interceptor;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.biz.api.context.ApiContext;
import com.ruoyi.biz.api.util.IpUtil;
/*import com.ruoyi.biz.domain.LogLogin;
import com.ruoyi.biz.service.ILogLoginService;*/
import com.ruoyi.common.constant.HttpStatus;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * 切面日志记录
 * @author 周周
 * @version 1.0.0
 */
@Aspect
@Component
@EnableAspectJAutoProxy
public class AspectLogInterceptor {
/*    private static final Logger logger = LoggerFactory.getLogger(AspectLogInterceptor.class);
    @Autowired
    private ILogLoginService logLoginService;
    *//**
     * 添加接口拦截
     *//*
    @Pointcut("execution(public * com.ruoyi.biz.api.controller.LoginApiController..*(..))")
    public void interceptMethods() {}
    *//**
     * 排除方法拦截
     *//*
    @Pointcut("execution(public * com.ruoyi.biz.api.controller.LoginApiController.smsCode*(..))")
    public void excludeMethods(){};
    *//**
     * 执行接口拦截
     *//*
    @Pointcut("interceptMethods() && !excludeMethods()")
    public void executeMethods(){};

    *//**
     * 注解执行拦截
     *//*
    @Pointcut("@annotation(com.ruoyi.biz.api.annotation.Log)")
    public void executeAnnotation(){};
    *//**
     * 拦截处理
     *//*
    @Around("executeAnnotation()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        // 获取当前拦截的方法签名
        String signatureShortStr = point.getSignature().toShortString();
        // 打印入参信息
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)requestAttributes;
        HttpServletRequest request = servletRequestAttributes.getRequest();
        Object result = point.proceed();
        Object[] args = point.getArgs();
        JSONObject paramObject = JSON.parseObject(JSON.toJSONString(args[0]));
        JSONObject resObject = JSON.parseObject(JSON.toJSONString(result));
        if (Integer.parseInt(resObject.getString("code")) == HttpStatus.SUCCESS){
            LogLogin login = new LogLogin();
            login.setUserId(Long.parseLong(resObject.getJSONObject("data").getString("id")));
            login.setIp(IpUtil.getIpAddr(request));
            login.setClient(paramObject.getString("client"));
            login.setEntry(paramObject.getString("entry"));
            login.setVersion(paramObject.getString("version"));
            int res = logLoginService.insertLogLogin(login);
            logger.info(res > 0 ? "log success" : "log error");
        }
        return result;
    }*/

}
